package com.example.demo.common;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-06-12
 * Time: 20:05
 */
@Component
public class CheckCodeTools {

    @Value("${imgpath}")
    private String imagePath;

    public String[] createImage() {

        // 定义图片名称
        String imageName = UUID.randomUUID().toString() + ".png";

        // 定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(320, 50);

        // 图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(imagePath + imageName);

        // 得到验证码
        String code = lineCaptcha.getCode();
        String[] result = new String[]{imageName, code};
        return result;
    }
}
